package com.cn.newetv.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.cn.newetv.po.SegDO;
import com.cn.newetv.po.VideoDO;

public class ParseYouKuParam {
	static String interString = "pid=143f8dcdfe8db081&guid=&ver=2.1&network=&format=1&rt=2";
	
	public static String converStreamToString(InputStream inputstream) throws Exception {
        BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream));
        StringBuilder stringbuilder = new StringBuilder();
        String s = null;
        do {
	        s = bufferedreader.readLine();
	        stringbuilder.append((new StringBuilder(String.valueOf(s))).append("\n").toString());
        } while(s != null);
        
        try {
        	inputstream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
        return stringbuilder.toString();
	}
	
	public static JSONObject transToJson(String strJson) throws Exception {
		if(strJson == null || strJson.length() == 0)
			return null;
		JSONObject jsonobj = new JSONObject(strJson);
		return jsonobj;
	}
	
	public static String connectAPI(String strurl) throws Exception {
		int resCode = 0;
		URL url = new URL(strurl);
		URLConnection urlConnection = url.openConnection();
		HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
		httpURLConnection.setAllowUserInteraction(false);
        httpURLConnection.setInstanceFollowRedirects(true);
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setRequestProperty("User-Agent", "");
        httpURLConnection.connect();
        resCode = httpURLConnection.getResponseCode();
        if (resCode != 200)
        	return null;
        else 
        	return converStreamToString(httpURLConnection.getInputStream());
        
	}
	
	public static VideoDO getVideoInfo(String videoId) {
		try {
			String url = constructURL(videoId);
			String strData = connectAPI(url);
			JSONObject json = transToJson(strData);
			System.out.println(strData);
			return constructDO(json);
		} catch (Exception e) {
			Log.e("study", e.getMessage());
		}
		return null;
	}
	
	public static VideoDO constructDO(JSONObject json) throws Exception {
		VideoDO videoDO = new VideoDO();
		List<SegDO> segs = new ArrayList<SegDO>();
		JSONObject streamPadMp4 = json.getJSONObject("results").getJSONObject("streamPadMp4");
		videoDO.setTotalseconds(streamPadMp4.getInt("totalseconds"));
		videoDO.setVname(json.getJSONObject("results").getString("showname"));
		JSONArray array = streamPadMp4.getJSONArray("segs");
		
		for (int i = 0; i < array.length(); i++) {
			JSONObject js = array.getJSONObject(i);
			SegDO segDO = new SegDO();
			segDO.setId(js.getInt("id"));
			segDO.setSeconds(js.getInt("seconds"));
			segDO.setSize(js.getInt("size"));
			segDO.setUrl(js.getString("url"));
			segs.add(segDO);
		}
		videoDO.setSegs(segs);
		return videoDO;
	}
	
	public static String constructURL(String videoId) {
		return (new StringBuilder("http://api.3g.youku.com/openapi-wireless/getVideoDetail?")).append(interString).append("&vid=").append(videoId).toString();
	}
	
	
}
